﻿using Abp.Extensions;
using Mt.Site.Core.Base.Utils.FileIO;
using System.Globalization;
using System.IO;

namespace Mt.Site.Web
{
    public static class AppWebConsts
    {
        public static string Angular_Localization
        {
            get
            {
                return GetLocalizationFileForjAngularOrNull(CultureInfo.CurrentUICulture.Name.ToLower())
                       ?? GetLocalizationFileForjAngularOrNull(CultureInfo.CurrentUICulture.Name.Left(2).ToLower())
                       ?? "/lib/angularjs/i18n/angular-locale_en-us.js";
            }
        }
        private static string GetLocalizationFileForjAngularOrNull(string cultureCode)
        {
            try
            {
                var relativeFilePath = "/lib/angularjs/i18n/angular-locale_" + cultureCode + ".js";
                var physicalFilePath = FileHelper.MapPath(relativeFilePath);
                if (File.Exists(physicalFilePath))
                {
                    return relativeFilePath;
                }
            }
            catch { }

            return null;
        }


        public static string JQuery_Validation_Localization
        {
            get
            {
                return GetLocalizationFileForjQueryValidationOrNull(CultureInfo.CurrentUICulture.Name.ToLower().Replace("-", "_"))
                       ?? GetLocalizationFileForjQueryValidationOrNull(CultureInfo.CurrentUICulture.Name.Left(2).ToLower())
                       ?? "/lib/jquery-validation/js/localization/_messages_empty.js";
            }
        }
        private static string GetLocalizationFileForjQueryValidationOrNull(string cultureCode)
        {
            try
            {
                var relativeFilePath = "/lib/jquery-validation/js/localization/messages_" + cultureCode + ".min.js";
                var physicalFilePath = FileHelper.MapPath(relativeFilePath);
                if (File.Exists(physicalFilePath))
                {
                    return relativeFilePath;
                }
            }
            catch { }

            return null;
        }

        public static string Ckeditor_Localization
        {
            get
            {
                var cultureName = CultureInfo.CurrentUICulture.Name.ToLower();
                if(GetLocalizationFileForCkeditorOrNull(cultureName) != null)
                {
                    return cultureName;
                }
                else
                {
                    cultureName = cultureName.Left(2);
                    if (GetLocalizationFileForCkeditorOrNull(cultureName) != null)
                    {
                        return cultureName;
                    }
                }
                return "en";
            }
        }
        private static string GetLocalizationFileForCkeditorOrNull(string cultureCode)
        {
            try
            {
                var relativeFilePath = "/lib/ckeditor/lang/" + cultureCode + ".js";
                var physicalFilePath = FileHelper.MapPath(relativeFilePath);
                if (File.Exists(physicalFilePath))
                {
                    return relativeFilePath;
                }
            }
            catch { }

            return null;
        }

        public static string Bootstrap_Select_Localization
        {
            get
            {
                return GetLocalizationFileForBootstrapSelect(CultureInfo.CurrentUICulture.Name.ToLower())
                       ?? GetLocalizationFileForBootstrapSelect(CultureInfo.CurrentUICulture.Name.Left(2).ToLower())
                       ?? "/lib/bootstrap-select/i18n/defaults-en_US.js";
            }
        }
        private static string GetLocalizationFileForBootstrapSelect(string cultureCode)
        {
            var localizationFileList = new[]
            {
                "ar_AR", 
                "bg_BG", 
                "cs_CZ", 
                "da_DK", 
                "de_DE", 
                "en_US", 
                "es_CL",
                "eu",
                "fa_IR",
                "fi_FI",
                "fr_FR",
                "hu_HU",
                "id_ID",
                "it_IT",
                "ko_KR",
                "nb_NO",
                "nl_NL",
                "pl_PL",
                "pt_BR",
                "pt_PT",
                "ro_RO",
                "ru_RU",
                "sk_SK",
                "sl_SL",
                "sv_SE",
                "tr_TR",
                "ua_UA",
                "zh_CN",
                "zh_TW"
            };

            try
            {
                cultureCode = cultureCode.Replace("-", "_");

                foreach (var localizationFile in localizationFileList)
                {
                    if (localizationFile.StartsWith(cultureCode))
                    {
                        return "/lib/bootstrap-select/i18n/defaults-" + localizationFile + ".js";
                    }
                }
            }
            catch { }

            return null;
        }

        public static string Bootstrap_Datepicker_Localization
        {
            get
            {
                var cultureName = CultureInfo.CurrentUICulture.Name;
                if (GetLocalizationFileForBootstrapDatepicker(cultureName) != null)
                {
                    return cultureName;
                }
                else
                {
                    cultureName = cultureName.Left(2);
                    if (GetLocalizationFileForBootstrapDatepicker(cultureName) != null)
                    {
                        return cultureName;
                    }
                }
                return "en";
            }
        }
        private static string GetLocalizationFileForBootstrapDatepicker(string cultureCode)
        {
            try
            {
                var relativeFilePath = "/lib/datepicker/locales/bootstrap-datepicker." + cultureCode + ".js";
                var physicalFilePath = FileHelper.MapPath(relativeFilePath);
                if (File.Exists(physicalFilePath))
                {
                    return relativeFilePath;
                }
            }
            catch { }

            return null;
        }

        public static string JQuery_Timeago_Localization
        {
            get
            {
                return GetLocalizationFileForjQueryTimeagoOrNull(CultureInfo.CurrentUICulture.Name.ToLower().Replace("-", "_"))
                       ?? GetLocalizationFileForjQueryTimeagoOrNull(CultureInfo.CurrentUICulture.Name.Left(2).ToLower())
                       ?? "/lib/jquery-timeago/locales/jquery.timeago.en.js";
            }
        }
        private static string GetLocalizationFileForjQueryTimeagoOrNull(string cultureCode)
        {
            try
            {
                var relativeFilePath = "/lib/jquery-timeago/locales/jquery.timeago." + cultureCode + ".js";
                var physicalFilePath = FileHelper.MapPath(relativeFilePath);
                if (File.Exists(physicalFilePath))
                {
                    return relativeFilePath;
                }
            }
            catch { }

            return null;
        }
    }
}